<template>
	<view class="user" :class="[isDark?'dark':'', $store.state.AppStyle]" :style="{'background-color':isDark?'#1c1c1c':'#f6f6f6', 'min-height':isDark?'100vh':'auto'}">
		<view class="header" :style="[{height:CustomBar + 'px'}]">
			<view class="cu-bar" :style="{'height': CustomBar + 'px','padding-top':StatusBar + 'px', 'background-color':isDark?'#1c1c1c':'#ffffff'}">
				<view class="action" @tap="back">
					<text class="cuIcon-back" :style="{'color':isDark?'#ffffff':'#000000'}"></text>
				</view>
				<view class="content text-bold" :style="[{top:StatusBar + 'px'}, {'color':isDark?'#ffffff':'#000000'}]">
					{{$t('user.setup.setuptitle')}}
				</view>
				<view class="action">
				</view>
			</view>
		</view>
		<view :style="[{padding:NavBar + 'px 10px 0px 10px'}]"></view>

		<view class="cu-list menu card-menu margin-top" :style="{'background-color':isDark?'#1c1c1c':'#ffffff'}">
			<block v-if="token!=''">
				<view class="cu-item" @tap="setInfo" :style="{'background-color':isDark?'#1c1c1c':'#ffffff'}">
					<view class="content">
						<text class="tn-icon-write  margin-right-sm" :style="{'color':isDark?'#ffffff':'#000000'}"></text>
						<text :style="{'color':isDark?'#ffffff':'#000000'}">{{$t('user.setup.editinfo')}}</text>
					</view>
					<view class="action">
						<text class="cuIcon-right" :style="{'color':isDark?'#ffffff':'#000000'}"></text>
					</view>
				</view>
				<view class="cu-item" @tap="setPw" :style="{'background-color':isDark?'#1c1c1c':'#ffffff'}">
					<view class="content">
						<text class="tn-icon-lock margin-right-sm" :style="{'color':isDark?'#ffffff':'#000000'}"></text>
						<text :style="{'color':isDark?'#ffffff':'#000000'}">{{$t('user.setup.editpassword')}}</text>
					</view>
					<view class="action">
						<text class="cuIcon-right" :style="{'color':isDark?'#ffffff':'#000000'}"></text>
					</view>
				</view>
				<view class="cu-item" @tap="setSafe" :style="{'background-color':isDark?'#1c1c1c':'#ffffff'}">
					<view class="content">
						<text class="tn-icon-safe  margin-right-sm" :style="{'color':isDark?'#ffffff':'#000000'}"></text>
						<text :style="{'color':isDark?'#ffffff':'#000000'}">{{$t('user.setup.accountsecurity')}}</text>
					</view>
					<view class="action">
						<text class="cuIcon-right" :style="{'color':isDark?'#ffffff':'#000000'}"></text>
					</view>
				</view>
				<view class="cu-item" @tap="toAddress" :style="{'background-color':isDark?'#1c1c1c':'#ffffff'}">
					<view class="content">
						<text class="tn-icon-map  margin-right-sm" :style="{'color':isDark?'#ffffff':'#000000'}"></text>
						<text :style="{'color':isDark?'#ffffff':'#000000'}">{{$t('user.setup.receivaddress')}}</text>
					</view>
					<view class="action">
						<text class="cuIcon-right" :style="{'color':isDark?'#ffffff':'#000000'}"></text>
					</view>
				</view>
				<view class="cu-item" @tap="toPay" :style="{'background-color':isDark?'#1c1c1c':'#ffffff'}">
					<view class="content">
						<text class="tn-icon-upload margin-right-sm" :style="{'color':isDark?'#ffffff':'#000000'}"></text>
						<text :style="{'color':isDark?'#ffffff':'#000000'}">{{$t('user.setup.cashoutset')}}</text>
					</view>
					<view class="action">
						<text class="cuIcon-right" :style="{'color':isDark?'#ffffff':'#000000'}"></text>
					</view>
				</view>
			</block>
		</view>
		<!-- <view class="cu-list menu margin-top">
				<block v-if="token!=''">
				<view class="cu-item" @tap="toRz">
					<view class="content">
						<text>蓝V申请</text>
					</view>
					<view class="action">
						<text class="cuIcon-right"></text>
					</view>
				</view>
				<view class="cu-item" @tap="toSm">
					<view class="content">
						<text>{{$t('global.realnameauth')}}</text>
					</view>
					<view class="action">
						<text class="cuIcon-right"></text>
					</view>
				</view>
				</block>
				</view> -->
		<view class="cu-list menu card-menu margin-top">
			<block v-if="token!=''">
				<view class="cu-item">
					<view class="content">
						<text class="tn-icon-reply  margin-right-sm"></text>
						<text>{{$t('user.setup.accountstatus')}}<text class="cuIcon-question margin-left-xs" @tap="showModal"
								data-target="Modal"></text></text>
					</view>
					<view class="action">
						<block v-if="systemBan==-1">
							<text class="text-blue">{{$t('user.setup.obtaining')}}</text>
						</block>
						<block v-if="systemBan==1">
							<text class="text-red">{{$t('user.setup.limited')}}</text>
						</block>
						<block v-if="systemBan==2">
							<text class="text-red">{{$t('user.setup.logoff')}}</text>
						</block>
						<block v-if="systemBan==0">
							<text class="text-green">{{$t('user.setup.normal')}}</text>
						</block>
					</view>
				</view>
				<view class="cu-item" @tap="showModal2" data-target="deleteTips">
					<view class="content">
						<text class="tn-icon-delete  margin-right-sm"></text>
						<text>{{$t('user.setup.logoutaccount')}}</text>
					</view>
					<view class="action">
						<text class="cuIcon-right"></text>
					</view>
				</view>
			</block>
			<view class="cu-item" @tap="callme">
				<view class="content">
					<text class="tn-icon-service  margin-right-sm"></text>
					<text>{{$t('user.setup.contactus')}}</text>
				</view>
				<view class="action">
					<text class="cuIcon-right"></text>
				</view>
			</view>
			<!-- #ifdef APP-PLUS || H5 -->
			<view class="cu-item" @tap="toDarkModeSettings" :style="{'background-color':isDark?'#1c1c1c':'#ffffff'}">
				<view class="content">
					<text class="tn-icon-moon margin-right-sm" :style="{'color':isDark?'#ffffff':'#000000'}"></text>
					<text :style="{'color':isDark?'#ffffff':'#000000'}">{{$t('user.setup.facade')}}</text>
				</view>
				<view class="action">
					<text class="cuIcon-right" :style="{'color':isDark?'#ffffff':'#000000'}"></text>
				</view>
			</view>
			<!-- #endif -->
			<view class="cu-item" @tap="yjfk">
				<view class="content">
					<text class="tn-icon-comment  margin-right-sm"></text>
					<text>{{$t('user.setup.feedback')}}</text>
				</view>
				<view class="action">
					<text class="cuIcon-right"></text>
				</view>
			</view>
			<view class="cu-item" @tap="rmlocal">
				<view class="content">
					<text class="tn-icon-clear  margin-right-sm"></text>
					<text>{{$t('user.setup.clearcache')}}</text>
				</view>
				<view class="action">
					<text class="text-gray">{{ localdata }}</text>
				</view>
			</view>
			<!-- #ifdef APP-PLUS -->
			<view class="cu-item" @tap="isUpdate(true)">
				<view class="content">
					<text class="tn-icon-download margin-right-sm"></text>
					<text>{{$t('user.setup.checkupdate')}}</text>
				</view>
				<view class="action">
					<text class="text-gray" v-if="Update==0">{{wgtVer}}</text>
					<text class="update-tips bg-red" v-if="Update==1">{{$t('user.setup.findnewversion')}}</text>
					<text class="cuIcon-right"></text>
				</view>
			</view>
			<!-- #endif -->
		</view>
		<view class="cu-list menu  card-menu margin-top">

			<view class="cu-item" v-if="token!=''" @tap="switchAccounts">
				<view class="content">
					<text class="tn-icon-sequence margin-right-sm"></text>
					<text>{{$t('user.setup.switchaccount')}}</text>
				</view>
				<view class="action">
					<text class="cuIcon-right text-black"></text>
				</view>
			</view>
			<view class="cu-item" v-if="token!=''" @tap="logout">
				<view class="content">
					<text class="tn-icon-logout margin-right-sm"></text>
					<text>{{$t('user.setup.logout')}}</text>
				</view>
				<view class="action">
					<text class="cuIcon-right text-black"></text>
				</view>
			</view>

		</view>
		<view class="cu-list menu  card-menu margin-top">
			<view class="cu-item" @tap="yhxy">
				<view class="content">
					<text class="tn-icon-my-circle margin-right-sm"></text>
					<text>{{$t('user.setup.useragreement')}}</text>
				</view>
				<view class="action">
					<text class="cuIcon-right"></text>
				</view>
			</view>
			<view class="cu-item" @tap="ysxy">
				<view class="content">
					<text class="tn-icon-safe margin-right-sm"></text>
					<text>{{$t('user.setup.privacyagree')}}</text>
				</view>
				<view class="action">
					<text class="cuIcon-right"></text>
				</view>
			</view>
		</view>
		<view class="cu-modal" :class="modalName=='Modal'?'show':''">
			<view class="cu-dialog">
				<view class="cu-bar bg-white justify-end">
					<view class="content">{{$t('user.setup.accostatusdesc')}}</view>
					<view class="action" @tap="hideModal">
						<text class="cuIcon-close text-red"></text>
					</view>
				</view>
				<view class="padding-xl text-left bg-white">
					{{$t('user.setup.accounthasfake')}}
					<text class="text-red text-bold">{{$t('user.setup.funlimitation')}}</text>
					{{$t('user.setup.durationstatus')}}
				</view>
			</view>
		</view>
		<view class="cu-modal" :class="modalName=='deleteTips'?'show':''">
			<view class="cu-dialog">
				<view class="cu-bar bg-white justify-end">
					<view class="content">{{$t('user.setup.warn')}}</view>
					<view class="action" @tap="hideModal">
						<text class="cuIcon-close text-red"></text>
					</view>
				</view>
				<view class="padding-xl text-left bg-white">
					{{$t('user.setup.afterlogoff')}}
				</view>
				<view class="padding text-center" style="background: white;">
					<text @tap="showModal" data-target="deleteUser">
						<tn-button :backgroundColor="isDark?'#2c2c2c':'#ff4a4d'" fontColor="#fff">
							{{$t('user.setup.logoffnow')}}
						</tn-button>
					</text>
					<tn-button style="margin-left: 20rpx;border: 2px solid #3cc9a4;color: #3cc9a4;" :backgroundColor="isDark?'#2c2c2c':'#ffffff'" @tap="hideModal" fontColor="#3cc9a4">
						{{$t('user.setup.thinkaboutit')}}
					</tn-button>
				</view>
			</view>
		</view>
		<view class="cu-modal bottom-modal" :class="modalName=='deleteUser'?'show':''">
			<view class="cu-dialog">
				<view class="cu-bar bg-white">
					<view class="action">{{$t('user.setup.logoutaccount')}}</view>
					<view class="action text-blue" @tap="hideModal">{{$t('common.cancel')}}</view>
				</view>
				<view class="padding-top padding-bottom text-left">
					<form>
						<view class="cu-form-group">
							<view class="title">{{$t('global.username')}}</view>
							<input name="input" disabled="disabled" :value="name"/>
						</view>
						<view class="all-btn">
							<view class="user-btn flex flex-direction">
								<button class="cu-btn bg-blue margin-tb-sm lg" @tap="selfDelete()">{{$t('user.setup.logoffnow')}}</button>
							</view>
						</view>
					</form>
				</view>
			</view>
		</view>
	</view>
</template>

<script>
	import {
		localStorage
	} from '../../js_sdk/mp-storage/mp-storage/index.js'
	import darkModeMixin from '@/utils/darkModeMixin.js';
	export default {
		mixins: [darkModeMixin],
		data() {
			return {
				StatusBar: this.StatusBar,
				CustomBar: this.CustomBar,
				NavBar: this.StatusBar + this.CustomBar,
				AppStyle: this.$store.state.AppStyle,

				modalName: null,

				localdata: '',
				systemBan: "-1",

				versionCode: 0,
				wgtVer: '',

				Update: 0,
				versionUrl: "",

				userInfo: null,
				token: "",
				code: '',
				name: ""
			}
		},
		onPullDownRefresh() {

		},
		onShow() {
			var that = this;
			// #ifdef APP-PLUS
			that.isUpdate(false);
			//plus.navigator.setStatusBarStyle("dark")
			that.getUserData();
			// #endif
			if (localStorage.getItem('isqh')) {
				localStorage.removeItem('isqh');
			}
			if (localStorage.getItem('userinfo')) {
				that.userInfo = JSON.parse(localStorage.getItem('userinfo'));
				that.name = that.userInfo.name;
			}
		},
		onLoad() {
			var that = this;
			// #ifdef APP-PLUS || MP
			that.NavBar = that.CustomBar;
			// #endif
			if (localStorage.getItem('token')) {

				that.token = localStorage.getItem('token');
			}
			that.get_cache_size();
			that.getUserData();
		},
		methods: {
			switchAccounts() {
				var that = this;
				if (!localStorage.getItem('token') || localStorage.getItem('token') == "") {
					uni.showToast({
						title: that.$t('common.plslogin'),
						icon: 'none'
					})
					return false;
				}
				//获取当前缓存
				var userinfo = null;
				try {
					if (localStorage.getItem('userinfo')) {
						userinfo = JSON.parse(localStorage.getItem('userinfo'));
					}
				} catch (e) {
					
				}
				//把当前用户载入本地缓存
				var localAccounts = [];
				try {
					if (localStorage.getItem('localAccounts')) {
						localAccounts = JSON.parse(localStorage.getItem('localAccounts'));
					}
				} catch (e) {
					
				}
				if (localAccounts.length > 0) {
					var isHave = 0
					for (var i in localAccounts) {
						if (localAccounts[i].uid == userinfo.uid) {
							isHave = 1;
							localAccounts[i] = userinfo;
						}
					}
					if (isHave == 0) {
						if (userinfo != null) {
							localAccounts.push(userinfo);
						}
					}
				} else {
					if (userinfo != null) {
						localAccounts.push(userinfo);
					}
				}
				localStorage.setItem('localAccounts', JSON.stringify(localAccounts));
				uni.navigateTo({
					url: '/pages/user/switchAccounts'
				});
			},
			replaceSpecialChar(text) {
				text = text.replace(/"/g, '');
				text = text.replace(/"/g, '');
				// text = text.replace(/&lt;/g, '<');
				// text = text.replace(/&gt;/g, '>');
				// text = text.replace(/&nbsp;/g, ' ');
				return text;
			},
			isUpdate(Status) {
				var that = this;
				plus.runtime.getProperty(plus.runtime.appid, function(inf) {
					that.wgtVer = inf.version //获取当前版本号
					that.versionCode = inf.versionCode;
					var version = inf.versionCode;
					that.$Net.request({
						url: that.$API.GetUpdateUrl(),
						method: 'get',
						success: function(res) {
							var versionCode = res.data.versionCode;
							that.versionUrl = res.data.versionUrl;
							if (Status) {
								uni.showToast({
									title: that.$t('user.setup.testfinish'),
									icon: 'none',
									duration: 1000,
									position: 'bottom',
								});
							}
							if (versionCode > version) {
								that.Update = 1;
								if (Status) {
									if (res.data.versionUrl != "") {
										plus.runtime.openURL(res.data.versionUrl);
									}
								}
							}
						},
						fail: function(res) {

						}
					})
				})
			},
			back() {
				uni.navigateBack({
					delta: 1
				});
			},
			showModal(e) {
				this.modalName = e.currentTarget.dataset.target
			},
			showModal2(e) {
				this.modalName = e.currentTarget.dataset.target
			},
			hideModal(e) {
				this.modalName = null
			},
			toRz() {
				var that = this;
				if (!localStorage.getItem('token') || localStorage.getItem('token') == "") {
					uni.showToast({
						title: that.$t('common.plslogin'),
						icon: 'none'
					})
					return false;
				}
				uni.navigateTo({
					url: '/pages/user/identifyblue'
				});
			},
			toSm() {
				var that = this;
				if (!localStorage.getItem('token') || localStorage.getItem('token') == "") {
					uni.showToast({
						title: that.$t('common.plslogin'),
						icon: 'none'
					})
					return false;
				}
				uni.navigateTo({
					url: '/pages/user/identify'
				});
			},
			toAddress() {
				uni.navigateTo({
					url: '/pages/user/address'
				});
			},
			ysxy() {
				uni.navigateTo({
					url: '/pages/user/privacy'
				});
			},
			yhxy() {
				uni.navigateTo({
					url: '/pages/user/agreement?button=none'
				});
			},
			callme() {
				uni.navigateTo({
					url: '/pages/user/media'
				});
			},
			yjfk() {
				uni.navigateTo({
					url: '/pages/user/help'
				});
			},

			setInfo() {
				var that = this;
				if (!localStorage.getItem('token') || localStorage.getItem('token') == "") {
					uni.showToast({
						title: that.$t('common.plslogin'),
						icon: 'none'
					})
					return false;
				}
				uni.navigateTo({
					url: '/pages/user/useredit?type=setInfo'
				});
			},
			setPw() {
				var that = this;
				if (!localStorage.getItem('token') || localStorage.getItem('token') == "") {
					uni.showToast({
						title: that.$t('common.plslogin'),
						icon: 'none'
					})
					return false;
				}
				uni.navigateTo({
					url: '/pages/user/useredit?type=setPw'
				});
			},
			setSafe() {
				var that = this;
				if (!localStorage.getItem('token') || localStorage.getItem('token') == "") {
					uni.showToast({
						title: that.$t('common.plslogin'),
						icon: 'none'
					})
					return false;
				}
				uni.navigateTo({
					url: '/pages/user/useredit?type=setSafe'
				});
			},
			rmlocal() {
				var that = this;
				uni.showModal({
					title: that.$t('global.prompt'),
					content: that.$t('user.setup.isclearcache'),
					success: function(res) {
						if (res.confirm) {
							that.closeLocal();
						} else if (res.cancel) {
							
						}
					}
				});
			},
			toPay() {
				uni.navigateTo({
					url: '/pages/user/pay'
				});
			},
			getUserData() {
				var that = this;
				that.$Net.request({
					url: that.$API.getUserData(),
					data: {
						"token": that.token
					},
					header: {
						'Content-Type': 'application/x-www-form-urlencoded'
					},
					method: "get",
					dataType: 'json',
					success: function(res) {
						if (res.data.code == 1) {
							that.systemBan = res.data.data.systemBan;
						}
					},
					fail: function(res) {
						uni.showToast({
							title: that.$t('common.networkfail'),
							icon: 'none'
						})
					}
				})
			},
			closeLocal() {
				var that = this
				var userinfo = "";
				var token = "";
				//绕过用户状态
				if (localStorage.getItem('userinfo')) {
					userinfo = localStorage.getItem('userinfo')
					token = localStorage.getItem('token');
				}
				localStorage.clear();
				uni.showToast({
					title: that.$t('global.opersuccess'),
					icon: 'none',
					duration: 1000,
					position: 'bottom'
				})
				that.localdata = '0 KB'
				localStorage.setItem('userinfo', userinfo);
				localStorage.setItem('token', token);
			},
			get_cache_size() {
				var that = this
				var obj = uni.getStorageInfoSync()
				if (obj) {
					that.localdata = obj.currentSize + ' KB'
				} else {
					
				}
			},

			logout() {
				var that = this;
				uni.showModal({
					title: that.$t('global.prompt'),
					content: that.$t('user.setup.surelogout'),
					success: function(res) {
						if (res.confirm) {
							uni.showLoading({
								title: that.$t('common.loadmore')
							});
							var data = {
								"token": that.token
							}
							that.$Net.request({
								url: that.$API.signOut(),
								data: data,
								header: {
									'Content-Type': 'application/x-www-form-urlencoded'
								},
								method: "get",
								dataType: 'json',
								success: function(res) {
									setTimeout(function() {
										uni.hideLoading();
									}, 1000);
									if (res.data.code == 1) {
										localStorage.removeItem('userinfo');
										localStorage.removeItem('token');
										uni.showToast({
											title: that.$t('user.setup.logoutsuccess'),
											icon: 'none',
											duration: 1000,
											position: 'bottom',
										});
										var timer = setTimeout(function() {
											that.back();
											clearTimeout('timer')
										}, 1000)
									} else {
										uni.showToast({
											title: res.data.msg,
											icon: 'none'
										})
									}
								},
								fail: function(res) {
									setTimeout(function() {
										uni.hideLoading();
									}, 1000);
									uni.showToast({
										title: that.$t('common.networkfail'),
										icon: 'none'
									})
								}
							})
						} else if (res.cancel) {

						}
					}
				});
			},
			selfDelete() {
				var that = this;
				var token = "";
				if (localStorage.getItem('userinfo')) {
					var userInfo = JSON.parse(localStorage.getItem('userinfo'));
					token = userInfo.token;
				}
				var data = {
					"token": token
				}
				that.hideModal();
				uni.showModal({
					title: that.$t('user.setup.surelogoffaccount'),
					success: function(res) {
						if (res.confirm) {
							uni.showLoading({
								title: that.$t('common.loadmore')
							});

							that.$Net.request({
								url: that.$API.selfDelete(),
								data: data,
								header: {
									'Content-Type': 'application/x-www-form-urlencoded'
								},
								method: "get",
								dataType: 'json',
								success: function(res) {
									setTimeout(function() {
										uni.hideLoading();
									}, 1000);
									uni.showToast({
										title: res.data.msg,
										icon: 'none'
									})
									if (res.data.code == 1) {
										// localStorage.removeItem('userinfo');
										// localStorage.removeItem('token');
										var timer = setTimeout(function() {
											that.getUserData();
											uni.redirectTo({
												url: '/pages/home/index'
											})
											clearTimeout('timer')
										}, 1000)
									}
								},
								fail: function(res) {
									setTimeout(function() {
										uni.hideLoading();
									}, 1000);
									uni.showToast({
										title: that.$t('common.networkfail'),
										icon: 'none'
									})
								}
							})
						} else if (res.cancel) {
							
						}
					}
				});
			},
			toDarkModeSettings() {
				uni.navigateTo({
					url: '/pages/user/darkmode'
				});
			}
		}
	}
</script>

<style>
</style>